//
//  LZMyCenterTableView.m
//  laziz_Merchant
//
//  Created by ZhaoDongBo on 2016/11/24.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZMyCenterTableView.h"
#import "LZMyPersonInfoCommonTableViewCell.h"
#import "LZMyCenterUserInfoTableViewCell.h"

#import "Masonry.h"

#import "LZUserModel.h"
#import "UIImageView+WebCache.h"
#import "LZMyTableViewButtonView.h"

@interface LZMyCenterTableView() <UITableViewDelegate, UITableViewDataSource,LZMyCenterUserInfoTableViewCellDelegate>
@property (nonatomic, strong) LZMyTableViewButtonView * buttonView;
@end

@implementation LZMyCenterTableView

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    self = [super initWithFrame:frame style:style];
    if (self) {
        [self setup];
    }
    return self;
}

#pragma
#pragma mark private method

- (void)setup {
    self.backgroundColor = UIColorFromRGB(0xf9f7f7,1);
    self.separatorColor = [UIColor clearColor];
    self.dataSource = self;
    self.delegate = self;
}

- (LZMyTableViewButtonView *)buttonView {
    
    if (_buttonView == nil) {
        _buttonView = [[LZMyTableViewButtonView alloc] initWithButtonType:LZ_MY_TABLEVIEW_BUTTON_TYPE_QUIT];
        
        WS(weakSelf);
        _buttonView.didButtonActionBlock = ^(UIButton *button){
            if (weakSelf.didButtonActionBlock) {
                weakSelf.didButtonActionBlock(button);
            }
        };
    }
    
    return _buttonView;
}

#pragma
#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 1;
    }
    else
        return self.dataArrays.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 1) {
        
        LZMyPersonInfoCommonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
        if (!cell) {
            cell = [[LZMyPersonInfoCommonTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                                            reuseIdentifier:@"CellIdentifier"
                                                                    andType:LZ_MY_INFO_CELL_TYPE_LINKACCOUNT];
        }
        cell.titleLabel.text = self.dataArrays[indexPath.row];
        cell.subTitleLabel.text = @"";
        cell.headImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"lz_mycentercell_%ld", (long)indexPath.row]];
        
        return cell;
    }
    else {
        LZMyCenterUserInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"userInfoCellIdentifier"];
        if (!cell) {
            cell = [[LZMyCenterUserInfoTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                                            reuseIdentifier:@"userInfoCellIdentifier"];
        }
        cell.delegate = self;
        cell.userNameLabel.text = [LZUserModel Instance].merchantInfo.restaurantName;
        [cell.userImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", APIBaseImageUrl, [LZUserModel Instance].merchantInfo.sellerPhoto]]
                              placeholderImage:[UIImage imageNamed:@"lz_userinfo_bg_headerimage"]];
        
        return cell;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 1) {
        return self.buttonView;
    }
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    if (section == 1) {
        return 120.0f;
    }
    
    return 0.001;
}

#pragma
#pragma mark UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        return 262;
    }
    else
        return 55.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if(_didSelectRowBlock) {
        _didSelectRowBlock(indexPath);
    }
}

#pragma mark - head cell delegate
- (void)clickMarkImageView
{
    if(_didSelectRowBlock) {
        _didSelectRowBlock([NSIndexPath indexPathForRow:0 inSection:0]);
    }
}
@end
